define(function(require, exports) {
	require("component/js/jquery-2.1.4.min.js");
	var framework = require("component/js/framework.js");
	require('component/js/choiceTime.js');
	//require("${appPath}/js/ajaxfileupload.js");
	framework.setCurrentMenu("PAGE_001");
	checkIsLogin();
	search();
	
	function checkIsLogin(){
		var admin = sessionStorage.getItem("admin");
	    $("#userNU .userC").find("dt").html(admin.loginName);
	    $("#userNU .userC").find("dt").attr("title",admin.loginName);
		if(admin==null || admin==""){
			window.location.href="http://localhost:8080/erhuogou/manager/login.html";
		}
	}
	// 查找函数
	function search() {
		var page = getHashPage();
		if (page == null) {
			page = 1;
		}
		searchHandler(page);
	}

	function searchHandler(page) {
		page = parseInt(page);
		window.location.hash = "#" + page;
		clearTable();
		initTable(page);
	}

	function clearTable() {
		$(".content .commonTb tr:gt(0)").detach();
		$(".content .paginator span").detach();
	}

	// 初始化列表
	function initTable(page) {
		framework.showLoading();
		/*
		 * var rows = Math.floor(($(window).height() - $('.topNav').height() -
		 * $('.content .noBorderB').height() - $('.content .searchD').height() -
		 * $('.content .commonTb tr:eq(0)').height() - 100 ) / 50); rows = rows <=
		 * 0 ? 1 : rows;
		 */
		var rows = 20;
		var id = $("#searchId").val();
		var oneCategoryId = $("#searchOneCateId").val();
		var twoCategoryId = $("#searchTwoCateId").val();
		var threeCategoryId = $("#searchThreeCateId").val();
		var provinceId = $("#searchProId").val();
		var cityId = $("#searchCityId").val();
		var schoolId = $("#searchSchId").val();
		var tradeWay = $("#searchTradeWay option:selected").val();
		var reportStatus = $("#searchReportStatus option:selected").val();
		var shieldStatus = $("#searchShieldStatus option:selected").val();
		var tradeStatus = $("#searchTradeStatus option:selected").val();
		var date = $("#datePicker").val();
		var startDate = $('input[id="startDate"]').val();
		var endDate = $('input[id="endDate"]').val();
		
		if(tradeWay=="2"){
			tradeWay = "";
		}
		if(reportStatus=="2"){
			reportStatus = "";
		}
		if(shieldStatus=="2"){
			shieldStatus = "";
		}
		if(tradeStatus=="2"){
			tradeStatus = "";
		}
		
		$.ajax({
			url:"commodityInfo_listPage.action",
			type:"get",
			dataType:"json",
			data:{
				"page":page,
				"rows":rows,
				"id":id,
				"oneCategoryId":oneCategoryId,
				"twoCategoryId":twoCategoryId,
				"threeCategoryId":threeCategoryId,
				"provinceId":provinceId,
				"cityId":cityId,
				"schoolId":schoolId,
				"tradeWay":tradeWay,
				"reportStatus":reportStatus,
				"shieldStatus":shieldStatus,
				"tradeStatus":tradeStatus,
				"startDate" : startDate,
				"endDate" : endDate
			},
			success:function(data){
				if (data.data != null && data.data.rows != null) {
					for (var index = 0; index < data.data.rows.length; index++) {
						initTableRow(data.data.rows[index], index + 1);
					}
					if (data.data.records <= rows) {
						$('.paginator').css("display", "none");
					} else {
						$('.paginator').css("display", "");
					}
					initFooder(page, rows, data.data.records);
				}
				framework.hideLoading();
			}
		});
	}

	// 初始化表行
	function initTableRow(rowObject, index) {
		var clazz = "";
		if (index % 2 == 0) {
			clazz = "alt";
		}

		var id = rowObject.id;
		var memberId = rowObject.memberId;
		var name = rowObject.name;
		var headimgUrl = rowObject.headimgUrl;
		var oriPrice = rowObject.oriPrice;
		var transferPrice = rowObject.transferPrice;
		var quality = rowObject.quality;
		var location = rowObject.location;
		var contactWay = rowObject.contactWay;
		var tradeWay = rowObject.tradeWay;
		var commendNum = rowObject.commendNum;
		var reportStatus = rowObject.reportStatus;
		var reportNum = rowObject.reportNum;
		var messagesNum = rowObject.messagesNum;
		var shieldStatus = rowObject.shieldStatus;
		var tradeStatus = rowObject.tradeStatus;
		var oneCategoryName = rowObject.oneCategoryName;
		var twoCategoryName = rowObject.twoCategoryName;
		var threeCategoryName = rowObject.threeCategoryName;
		var oneCategoryId = rowObject.oneCategoryId;
		var twoCategoryId = rowObject.twoCategoryId;
		var threeCategoryId = rowObject.threeCategoryId;
		var provinceId = rowObject.provinceId;
		var cityId = rowObject.cityId;
		var schoolId = rowObject.schoolId;
		var issueTime = rowObject.issueTime;
		var editTime = rowObject.editTime;
		var detail = rowObject.detail;
		
		var _shieldStatus = "";
		if (shieldStatus=="0"){
			_shieldStatus = "开启";
		}else if(shieldStatus=="1"){
			_shieldStatus = "屏蔽";
		}
		
		
		var _tradeStatus = "";
		if (tradeStatus=="0"){
			_tradeStatus = "出售中";
		}else if(tradeStatus=="1"){
			_tradeStatus = "已出售";
		}
		
		var _quality = "";
		if(quality=="50"){
			_quality = "五成新";
		}else if(quality=="60"){
			_quality = "六成新";
		}else if(quality=="70"){
			_quality = "七成新";
		}else if(quality=="80"){
			_quality = "八成新";
		}else if(quality=="90"){
			_quality = "九成新";
		}else if(quality=="95"){
			_quality = "九五新";
		}else{
			_quality = "全新";
		}
		
		var _tradeWay = "";
		if(tradeWay=="0"){
			_tradeWay = "线上交易";
		}else if(tradeWay=="1"){
			_tradeWay = "当面交易";
		}
		
		var _reportStatus = "";
		if(reportStatus=="0"){
			_reportStatus = "未被举报";
		}else if(reportStatus=="1"){
			_reportStatus = "被举报";
		}

		var row = $("<tr/>");
		var tdDiv = "";
		if(shieldStatus=="0"){
			tdDiv += '<div class="editM1" title="屏蔽"><a href="javascript:void(0);">屏蔽</a></div>';
		}else if(shieldStatus=="1"){
			tdDiv += '<div class="editM1" title="启用"><a href="javascript:void(0);">启用</a></div>';
		}
		row.append('<td class="operateTd_d" style="line-height: 28px;" id="_operateTd_'
				+ index + '">' + tdDiv + '</td>');
		
		var img = "<img class='viewImg' src='"+headimgUrl+"'  height='20px'/>"
		row.append('<td title="'+ id +'" style="text-align:center">'+ id +'</>');
		row.append('<td title="'+ memberId +'" style="text-align:center">'+ memberId +'</>');
		row.append('<td title="'+ _shieldStatus +'" style="text-align:center">'+ _shieldStatus +'</>');
		row.append('<td title="'+ name +'" style="text-align:center">'+ name +'</>');
		row.append('<td title="'+ headimgUrl +'" style="text-align:center">'+ img +'</>');
		row.append('<td title="'+ oriPrice +'" style="text-align:center">'+ oriPrice +'</>');
		row.append('<td title="'+ transferPrice +'" style="text-align:center">'+ transferPrice +'</>');
		row.append('<td title="'+ _quality +'" style="text-align:center">'+ _quality +'</>');
		row.append('<td title="'+ location +'" style="text-align:center">'+ location +'</>');
		row.append('<td title="'+ contactWay +'" style="text-align:center">'+ contactWay +'</>');
		row.append('<td title="'+ _tradeWay +'" style="text-align:center">'+ _tradeWay +'</>');
		row.append('<td title="'+ commendNum +'" style="text-align:center">'+ commendNum +'</>');
		row.append('<td title="'+ _reportStatus +'" style="text-align:center">'+ _reportStatus +'</>');
		row.append('<td title="'+ reportNum +'" style="text-align:center">'+ reportNum +'</>');
		row.append('<td title="'+ messagesNum +'" style="text-align:center">'+ messagesNum +'</>');
		row.append('<td title="'+ _tradeStatus +'" style="text-align:center">'+ _tradeStatus +'</>');
		row.append('<td title="'+ oneCategoryName +'" style="text-align:center">'+ oneCategoryName +'</>');
		row.append('<td title="'+ twoCategoryName +'" style="text-align:center">'+ twoCategoryName +'</>');
		row.append('<td title="'+ threeCategoryName +'" style="text-align:center">'+ threeCategoryName +'</>');
		row.append('<td title="'+ oneCategoryId +'" style="text-align:center">'+ oneCategoryId +'</>');
		row.append('<td title="'+ twoCategoryId +'" style="text-align:center">'+ twoCategoryId +'</>');
		row.append('<td title="'+ threeCategoryId +'" style="text-align:center">'+ threeCategoryId +'</>');
		row.append('<td title="'+ provinceId +'" style="text-align:center">'+ provinceId +'</>');
		row.append('<td title="'+ cityId +'" style="text-align:center">'+ cityId +'</>');
		row.append('<td title="'+ schoolId +'" style="text-align:center">'+ schoolId +'</>');
		row.append('<td title="'+ issueTime +'" style="text-align:center">'+ issueTime +'</>');
		row.append('<td title="'+ editTime +'" style="text-align:center">'+ editTime +'</>');
		row.append('<td title="'+ detail +'" style="text-align:center">'+ detail +'</>');
		
		row.addClass(clazz);
		row.hover(function() {
			row.removeClass(clazz);
			row.addClass("over");
		}, function() {
			row.removeClass("over");
			row.addClass(clazz);
		});
		row.attr("commodityInfoId", id);
		$(".content .commonTb").append(row);

		//点击一级类别名跳转到二级分类列表
        row.find("._jump").click(function(){
            window.location.href="manager/twoCategoryList.html?oneCategoryId="+id;
        });

	    function IsNum(num){
	    	var reNum=/^\d*$/;
	    	return(reNum.test(num));
		}

	    // 屏蔽点击事件
		$("#_operateTd_" + index + " .editM1").click(function() {
			var transStatus = "";
			var _operate = "";
			if(shieldStatus=="0"){
				transStatus = 1;
				_operate = "屏蔽";
			}else if(shieldStatus=="1"){
				transStatus = 0;
				_operate = "启用";
			}
			framework.confirmss("确定"+_operate+"ID为:["+id+"]的二货?",
			function(){
				$.ajax({
					url:"commodityInfo_updateStatusById.action",
					type:"get",
					data:{"id":id,"status":transStatus},
					success:function(data){
						if(data.result=="SUCCESS"){
							search();
							framework.hint(_operate+"成功");
						}else{
							search();
							framework.hint(_operate+"失败");
						}
					}
				})
			},function(){
				
			})
		});
		
	}
	
	// 初始化脚
	// 参数： 第几页 行数 数量
	function initFooder(page, rows, count) {
		$('.content .paginator').append(
				'<span class="prev">上一页</span> '
						+ ' <span class="next">下一页</span>'
						+ '<span class="right">共</span> '
						+ '<div class="clear"></div>');
		var pageCount = Math.ceil(count / rows);
		var spanArray = new Array();
		spanArray.push(page);
		var i = 1;
		var mix = 5 > pageCount ? pageCount : 5;
		while (spanArray.length < mix) {
			if (page + i <= pageCount) {
				spanArray.push(page + i);
			}
			if (page - i > 0) {
				spanArray.unshift(page - i);
			}
			i++;
		}
		var firstCurrent = '';
		var lastCurrent = '';
		if (page == pageCount) {
			lastCurrent = 'class="current"';
		}
		if (page == 1) {
			firstCurrent = 'class="current"';
		}
		if (page <= pageCount - 3) {
			$('.content .paginator .prev:last').after(
					$('<span ' + lastCurrent + '></span>').append(
							initFooderPage(pageCount, pageCount))).after(
					$('<span></span>').append('...'));
		} else {
			$('.content .paginator .prev:last').after(
					$('<span ' + lastCurrent + '></span>').append(
							initFooderPage(pageCount, pageCount)));
		}
		for (var index = spanArray.length - 2; index >= 1; index--) {
			if (spanArray[index] == page) {
				$('.content .paginator .prev:last').after(
						$('<span class="current">' + page + '</span>'));
			} else {
				$('.content .paginator .prev:last').after(
						$('<span></span>').append(
								initFooderPage(spanArray[index],
										spanArray[index])));
			}
		}
		if (page > 3) {
			$('.content .paginator .prev:last').after(
					$('<span ' + firstCurrent + '></span>').append('...'))
					.after($('<span></span>').append(initFooderPage(1, 1)));
		} else {
			$('.content .paginator .prev:last').after(
					$('<span ' + firstCurrent + '></span>').append(
							initFooderPage(1, 1)));
		}

		if (page != 1) {
			$('.content .paginator .prev:first').html(
					initFooderPage(page - 1, '上一页'));
		}

		if (page != pageCount) {
			$('.content .paginator .next:first').html(
					initFooderPage(page + 1, '下一页'));
		}
		$('.content .paginator a').click(function() {
			searchHandler(parseInt($(this).attr('page')));
		});

		$('.content .paginator .right:last')
				.before(
						$('<span class="leftbd20"><img id="pgSubmit" src="./manager/images/footer_submit.png"></span>'))
				.before($('<span class="leftbd7"></span>').append('页'))
				.before(
						$('<span class="leftbd7"><input type="text" id="pgnum" class="text40" value="'
								+ page + '"></span>')).before(
						$('<span class="leftbd20"></span>').append('转到第'))
				.before($('<span class="leftbd2"></span>').append('条')).before(
						$('<span class="pageNum"></span>').append(count));

		$('#pgSubmit').click(function() {
			var pgnum = parseInt($('#pgnum').val());
			if (pgnum >= 1 && pgnum <= pageCount) {
				searchHandler(pgnum);
			} else {
				framework.hint("超出页码范围，一共" + pageCount + "页");
			}
		});
	}

	// 初始化点击脚的内容
	function initFooderPage(page, name) {
		var st = '<a href="javascript:void(0);" page=' + page + '>' + name
				+ '</a>';
		return st;
	}

	function longTrans2Date(time) {
		if (time == null || time == "")
			return "";
		var newTime = new Date(time);
		return newTime.Format("yyyy-MM-dd hh:mm:ss");
	}

	exports.searchList = function() {
		search();
	}

	$(window).bind('hashchange',function(e) {
		if ($('.content .paginator .current').html() == null) {
			return;
		}
		var hashPage = getHashPage();
		if (hashPage == null) {
			return;
		}
		if ($('.content .paginator .current').html()&& $('.content .paginator .current').html() != hashPage) {
			searchHandler(hashPage);
		}
	});

	function getHashPage() {
		var page = null;
		if (window.location.hash) {
			page = window.location.hash;
			if (page.indexOf("#") != -1) {
				page = page.substring(1);
			}
		}
		return page;
	}

	//搜索
	$("#searchListButton").click(function(){
		clearTable();
		searchHandler(1);
	});
	
	//清除搜索条件
	$("#clearBar").click(function(){
		$("#searchId").val("");
		$("#searchOneCateId").val("");
		$("#searchTwoCateId").val("");
		$("#searchThreeCateId").val("");
		$("#searchProId").val("");
		$("#searchCityId").val("");
		$("#searchSchId").val("");
		$("#searchTradeWay").find("option[value='2']").attr("selected","selected");
		$("#searchReportStatus").find("option[value='2']").attr("selected","selected");
		$("#searchShieldStatus").find("option[value='2']").attr("selected","selected");
		$("#searchTradeStatus").find("option[value='2']").attr("selected","selected");
		$("#startDate").val("");
		$("#endDate").val("");
		$('#datePicker').data('dateRangePicker').clear();
	});
	
});
